
const dateFtt = function (fmt, time) {
  let date = new Date(time.replace(/-/g,"/"));
  let o = {
    "M+": date.getMonth() + 1,                 //月份
    "d+": date.getDate(),                    //日
    "h+": date.getHours(),                   //小时
    "m+": date.getMinutes(),                 //分
    "s+": date.getSeconds(),                 //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
const toDecimal = function (x) {
  let f = 0;
  if (isNaN(x)) {
    return;
  }
  f = Math.round(x * 100) / 100;
  return f;
}
const getFileName=function(url){
  if(url){
    return url.substring(url.lastIndexOf('/')+1)
  }
}
const getSpanArr = function (rows,a) {
  let spanArr = [], pos = 0
  for (var i = 0; i < rows.length; i++) {
    if (i === 0) {
      spanArr.push(1);
      pos = 0;
    } else {
      // 判断当前元素与上一个元素是否相同(第1和第2列)
      if (this.rows[i][a]=== this.rows[i - 1][a]) {
        spanArr[pos] += 1;
        spanArr.push(0);
      } else {
        spanArr.push(1);
        pos = i;
      }

    }
  }
  return spanArr
}
const getQueryData=function(url) {
  url=url.split("?")[1]
  let result = {};
  if(url){
    let str = url;
    let strs = str.split("&");
    for (let i = 0; i < strs.length; i++) {
      result[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
    }
  }
  return result;
}
  const IEVersion=function() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  if(isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if(fIEVersion == 7) {
      return 7;
    } else if(fIEVersion == 8) {
      return 8;
    } else if(fIEVersion == 9) {
      return 9;
    } else if(fIEVersion == 10) {
      return 10;
    } else {
      return 6;//IE版本<=7
    }
  } else if(isEdge) {
    return 'edge';//edge
  } else if(isIE11) {
    return 11; //IE11
  }else{
    return -1;//不是ie浏览器
  }
}
const sortByKey=function(array,key){
  return array.sort(function(a,b){
    var x=a[key];
    var y=b[key];
    return ((x<y)?-1:((x>y)?1:0));
  });
}
module.exports  = {
  dateFtt: dateFtt,
  toDecimal: toDecimal,
  getSpanArr: getSpanArr,
  getFileName: getFileName,
  IEVersion: IEVersion,
  sortByKey: sortByKey,
}

